<?php

/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Controller;

use App\Entity\Result;
use App\Model\Device;
use App\Model\DevicePublish;
use App\Service\DeviceMqttService;
use App\Service\MqttClientService;
use App\Utils\Logger;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\Redis\Redis;
use Hyperf\Utils\ApplicationContext;
use Simps\MQTT\Client;
use Swoole\Coroutine;
use App\Model\Normal;


/**
 * Class IndexController
 *
 * @package App\Controller
 * @Controller()
 */
class IndexController extends AbstractController
{

    /**
     * @Inject
     * @var DeviceMqttService
     */
    protected DeviceMqttService $deviceMqttService;

    protected Redis $redis;


    public function __construct()
    {
        $this->redis = ApplicationContext::getContainer()->get(Redis::class);
    }

    public function index()
    {
        $user = $this->request->input('user', 'Hyperf');
        $method = $this->request->getMethod();

        return [
            'method' => $method,
            'message' => "Hello {$user}.",
        ];
    }

    public function statusQuery()
    {
        $mac = $this->request->input('mac');
        $test = $this->request->input('test') ?? 'no';
        Logger::get()->notice('statusQuery', [$test, $mac]);
        $device = Device::where('mac', $mac)->first();

        if ($device->clientId) {
            # 下发查询命令
            $cmd = $device->mac . "0C";
            $this->sendCmd($device->id, $device->clientId, $cmd);
        }

        return 'ok';
    }

    # 拉闸
    public function stop()
    {
        $mac = $this->request->input('mac');
        $device = Device::where('mac', $mac)->first();

        if ($device->clientId) {
            if ($device->mMac) {
                // $cache = $this->container->get(\Psr\SimpleCache\CacheInterface::class);
                // go(function () use ($mac, $device, $cache) {
                //     # 下发连接命令
                //     $cmd = $device->mac . "10" . "01";
                //     $this->sendCmd($device->id, $device->clientId, $cmd);
                //     $cache->delete($mac . '-close');
                //     //sleep(1);
                //     # 查询连接状态
                //     //$this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "02");
                //     $loop = 0;
                //     #$isDisconnected = false;
                //     $isSend = false;
                //     while ($loop < 30) {
                //         // sleep(1);
                //         Coroutine::sleep(1);
                //         $loop ++;

                //         Logger::get()->notice('mac state', [$cache->get($mac)]);
                //         if ($cache->get($mac) == '01' && !$isSend) {
                            # 下发拉闸命令
                            $data = "0D" . "02";
                            $cmd = $device->mac . "10" . "03" . $this->deviceMqttService->toLen(strlen($data) / 2, 2) . $data;
                            $this->sendCmd($device->id, $device->clientId, $cmd, 2);
                            $isSend = true;
                //             $cache->delete($mac);
                //             # 查询
                //             #sleep(1);
                //             #$cmd = $device->mac . "0C";
                //             #$this->sendCmd($device->id, $device->clientId, $cmd);
                //             //break;
                //         }
                //         if (!$isSend) {
                //             # 下发连接命令
                //             $this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "01");
                //         }
                //         if ($loop >= 30 && !$cache->get($mac . '-close')) {
                //             # 下发断开连接
                //             $this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "00");
                //         }
                //         if ($isSend && $cache->get($mac . '-close')) {
                //             break;
                //         }
                //     }
                // });
            } else {
                # 下发拉闸命令
                $cmd = $device->mac . "0D" . "02";
                $this->sendCmd($device->id, $device->clientId, $cmd);
                // sleep(1);
                # 查询
                Coroutine::sleep(1);
                $cmd = $device->mac . "0C";
                $this->sendCmd($device->id, $device->clientId, $cmd);
            }
        }

        return 'ok';
    }

    # 合闸
    public function start()
    {
        $mac = $this->request->input('mac');
        $device = Device::where('mac', $mac)->first();

        if ($device->clientId) {
            if ($device->mMac) {
                // $cache = $this->container->get(\Psr\SimpleCache\CacheInterface::class);
                // go(function () use ($mac, $device, $cache) {
                //     # 下发连接命令
                //     $this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "01");
                //     $cache->delete($mac . '-close');
                //     #sleep(1);
                //     # 查询连接状态
                //     #$this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "02");
                //     $loop = 0;
                //     #$isDisconnected = false;
                //     $isSend = false;
                //     while ($loop < 30) {
                //         // sleep(1);
                //         Coroutine::sleep(1);
                //         $loop ++;
                //         Logger::get()->notice('mac state', [$cache->get($mac)]);
                //         if ($cache->get($mac) == '01' && !$isSend) {
                            # 下发合闸命令
                            $data = "0D" . "01";
                            $cmd = $device->mac . "10" . "03" . $this->deviceMqttService->toLen(strlen($data) / 2, 2) . $data;
                            $this->sendCmd($device->id, $device->clientId, $cmd, 2);
                            $isSend = true;
                //             $cache->delete($mac);
                //             # 查询
                //             #sleep(1);
                //             #$cmd = $device->mac . "0C";
                //             #$this->sendCmd($device->id, $device->clientId, $cmd);
                //             #break;
                //         }
                //         if (!$isSend) {
                //             # 下发连接命令
                //             $this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "01");
                //         }
                //         if ($loop >= 30 && !$cache->get($mac . '-close')) {
                //             # 下发断开连接
                //             $this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "00");
                //         }
                //         if ($isSend && $cache->get($mac . '-close')) {
                //             break;
                //         }
                //     }
                // });

            } else {
                # 下发合闸命令
                $cmd = $device->mac . "0D" . "01";
                $this->sendCmd($device->id, $device->clientId, $cmd);

                # 查询
                // sleep(1);
                Coroutine::sleep(1);
                $cmd = $device->mac . "0C";
                $this->sendCmd($device->id, $device->clientId, $cmd);
            }
        }

        return 'ok';
    }

    # 重启
    public function restart()
    {
        $mac = $this->request->input('mac');
        $device = Device::where('mac', $mac)->first();

        if ($device->clientId) {
            if ($device->mMac) {
                // $cache = $this->container->get(\Psr\SimpleCache\CacheInterface::class);
                // go(function () use ($mac, $device, $cache) {
                //     # 下发连接命令
                //     $this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "01");
                //     $cache->delete($mac . '-close');
                //     #sleep(1);
                //     # 查询连接状态
                //     #$this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "02");
                //     $loop = 0;
                //     #$isDisconnected = false;
                //     $isSend = false;
                //     while ($loop < 30) {
                //         // sleep(1);
                //         Coroutine::sleep(1);
                //         $loop ++;
                //         Logger::get()->notice('mac state', [$cache->get($mac)]);
                //         if ($cache->get($mac) == '01' && !$isSend) {
                            # 下发合闸命令
                            $data = "03";
                            $cmd = $device->mac . "10" . "03" . $this->deviceMqttService->toLen(strlen($data) / 2, 2) . $data;
                            $this->sendCmd($device->id, $device->clientId, $cmd, 2);
                            $isSend = true;
                //             $cache->delete($mac);
                //             # 查询
                //             #sleep(1);
                //             #$cmd = $device->mac . "0C";
                //             #$this->sendCmd($device->id, $device->clientId, $cmd);
                //             #break;
                //         }
                //         if (!$isSend) {
                //             # 下发连接命令
                //             $this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "01");
                //         }
                //         if ($loop >= 30 && !$cache->get($mac . '-close')) {
                //             # 下发断开连接
                //             $this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "00");
                //         }
                //         if ($isSend && $cache->get($mac . '-close')) {
                //             break;
                //         }
                //     }
                // });

            } else {
                # 下发合闸命令
                $cmd = $device->mac . "03" ;
                $this->sendCmd($device->id, $device->clientId, $cmd);

                # 查询
                // sleep(1);
                Coroutine::sleep(1);
                $cmd = $device->mac . "0C";
                $this->sendCmd($device->id, $device->clientId, $cmd);
            }
        }

        return 'ok';
    }

    public function finalDu()
    {
        $mac = $this->request->input('mac');
        $du = (int)$this->request->input('du');
        $device = Device::where('mac', $mac)->first();

        if ($device->clientId) {
            if ($device->mMac) {
                # 下发充值命令
                $du = strtoupper(dechex($du));
                $data = "0A" . "03" . $this->deviceMqttService->toLen($du, 8);
                $cmd = $device->mac . "10" . "03" . $this->deviceMqttService->toLen(strlen($data) / 2, 2) . $data;
                $this->sendCmd($device->id, $device->clientId, $cmd, 0);
                $isSend = true;
            } else {
                # 下发充值命令
                $du = strtoupper(dechex($du));
                $cmd = $device->mac . "0A" . "03" . $this->deviceMqttService->toLen($du, 8);
                $this->sendCmd($device->id, $device->clientId, $cmd, 0);
                # 查询
                //$this->sendCmd($device->id, $device->clientId, $device->mac . "0C");
            }
        }

        return 'ok';
    }


    # 修改电量
    public function editDu()
    {
        $mac = $this->request->input('mac');
        $du = (int) $this->request->input('du');
        $device = Device::where('mac', $mac)->first();

        if ($device->clientId) {
            if ($device->mMac) {
                // $cache = $this->container->get(\Psr\SimpleCache\CacheInterface::class);
                // go(function () use ($mac, $device, $cache, $du) {
                //     # 下发连接命令
                //     $this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "01");
                //     $cache->delete($mac . '-close');
                //     #sleep(1);
                //     # 查询连接状态
                //     #$this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "02");
                //     $loop = 0;
                //     #$isDisconnected = false;
                //     $isSend = false;
                //     while ($loop < 30) {
                //         // sleep(1);
                //         Coroutine::sleep(1);
                //         $loop ++;
                //         Logger::get()->notice('mac state', [$cache->get($mac)]);
                //         if ($cache->get($mac) == '01' && !$isSend) {
                            # 下发充值命令
                            $du = strtoupper(dechex($du));
                            $data = "0A" . "02" . $this->deviceMqttService->toLen($du, 8);
                            $cmd = $device->mac . "10" . "03" . $this->deviceMqttService->toLen(strlen($data) / 2, 2) . $data;
                            $this->sendCmd($device->id, $device->clientId, $cmd, 0);
                            $isSend = true;
                //             $cache->delete($mac);
                //                   // 满足条件后立即跳出循环
                //             break;
                //             # 查询
                //             #sleep(1);
                //             #$cmd = $device->mac . "0C";
                //             #$this->sendCmd($device->id, $device->clientId, $cmd);
                //             #break;
                //         }
                //         if (!$isSend && ($loop == 1 || $loop == 10 || $loop == 20)) {
                //             # 下发连接命令
                //             $this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "01");
                //         }
                //         if ($loop >= 30 && !$cache->get($mac . '-close')) {
                //             # 下发断开连接
                //             $this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "00");
                //         }
                //         if ($isSend && $cache->get($mac . '-close')) {
                //             break;
                //         }
                //     }
                // });

            } else {
                # 下发充值命令
                $du = strtoupper(dechex($du));
                $cmd = $device->mac . "0A" . "02" . $this->deviceMqttService->toLen($du, 8);
                $this->sendCmd($device->id, $device->clientId, $cmd, 0);
                # 查询
                //$this->sendCmd($device->id, $device->clientId, $device->mac . "0C");
            }
        }

        return 'ok';
    }


    # 充电
    public function recharge()
    {
        $mac = $this->request->input('mac');
        $du = (int) $this->request->input('du');
        $device = Device::where('mac', $mac)->first();

        if ($device->clientId) {
            if ($device->mMac) {

                $cache = $this->container->get(\Psr\SimpleCache\CacheInterface::class);
                go(function () use ($mac, $device, $cache, $du) {
                    # 下发连接命令
                    // $this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "01");
                    // $cache->delete($mac . '-close');
                    // #sleep(1);
                    // # 查询连接状态
                    // #$this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "02");
                    // $loop = 0;
                    // #$isDisconnected = false;
                    // $isSend = false;
                    // while ($loop < 30) {
                    //     // sleep(1);
                    //     Coroutine::sleep(1);
                    //     $loop ++;
                    //     Logger::get()->notice('mac state', [$cache->get($mac)]);
                    //     if ($cache->get($mac) == '01' && !$isSend) {
                            # 下发充值命令
                            $du = strtoupper(dechex($du));
                            $data = "0A" . "01" . $this->deviceMqttService->toLen($du, 8);
                            $cmd = $device->mac . "10" . "03" . $this->deviceMqttService->toLen(strlen($data) / 2, 2) . $data;
                            $this->sendCmd($device->id, $device->clientId, $cmd, 0);
                            // $isSend = true;
                            // $cache->delete($mac);
                                   // 满足条件后立即跳出循环
                    //         break;
                    //         # 查询
                    //         #sleep(1);
                    //         #$cmd = $device->mac . "0C";
                    //         #$this->sendCmd($device->id, $device->clientId, $cmd);
                    //         #break;
                    //     }
                    //     if (!$isSend && ($loop == 8 || $loop == 15)) {
                    //         # 下发连接命令
                    //         $this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "01");
                    //     }
                    //     if ($loop >= 30 && !$cache->get($mac . '-close')) {
                    //         # 下发断开连接
                    //         $this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "00");
                    //     }
                    //     if ($isSend && $cache->get($mac . '-close')) {
                    //         break;
                    //     }
                    // }
                });

            } else {
                # 下发充值命令
                $du = strtoupper(dechex($du));
                $cmd = $device->mac . "0A" . "01" . $this->deviceMqttService->toLen($du, 8);
                $this->sendCmd($device->id, $device->clientId, $cmd, 0);
                # 查询
                //$this->sendCmd($device->id, $device->clientId, $device->mac . "0C");
            }
        }

        return 'ok';
    }

    # 清0
    public function clear()
    {
        $mac = $this->request->input('mac');
        $type = $this->request->input('type') ?? 1;
        $device = Device::where('mac', $mac)->first();

        if ($device->clientId) {
            if ($device->mMac) {
                // $cache = $this->container->get(\Psr\SimpleCache\CacheInterface::class);
                // go(function () use ($mac, $device, $cache, $type) {
                //     # 下发连接命令
                //     $this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "01");
                //     $cache->delete($mac . '-close');
                //     #sleep(1);
                //     # 查询连接状态
                //     #$this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "02");
                //     #$isDisconnected = false;
                //     $loop = 0;
                //     $isSend = false;
                //     while ($loop < 30) {
                //         // sleep(1);
                //         Coroutine::sleep(1);
                //         $loop ++;
                //         Logger::get()->notice('mac state', [$cache->get($mac)]);
                //         if ($cache->get($mac) == '01' && !$isSend) {
                            # 下发清 0 命令
                            $du = strtoupper(dechex(0));
                            if ($type == 2) {
                                $du = strtoupper(dechex(1000));
                            }
                            $data = "0A" . "03" . $this->deviceMqttService->toLen($du, 8);
                            $cmd = $device->mac . "10" . "03" . $this->deviceMqttService->toLen(strlen($data) / 2, 2) . $data;
                            $this->sendCmd($device->id, $device->clientId, $cmd);
                            $isSend = true;
                //             $cache->delete($mac);
                //             # 查询
                //             #sleep(1);
                //             #$cmd = $device->mac . "0C";
                //             #$this->sendCmd($device->id, $device->clientId, $cmd);
                //             #break;
                //         }
                //         if (!$isSend) {
                //             # 下发连接命令
                //             $this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "01");
                //         }
                //         if ($loop >= 30 && !$cache->get($mac . '-close')) {
                //             # 下发断开连接
                //             $this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "00");
                //         }
                //         if ($isSend && $cache->get($mac . '-close')) {
                //             break;
                //         }
                //     }
                // });
            } else {
                # 下发清0命令
                $du = strtoupper(dechex(0));
                if ($type == 2) {
                    $du = strtoupper(dechex(1000));
                }
                $cmd = $device->mac . "0A" . "03" . $this->deviceMqttService->toLen($du, 8);
                $this->sendCmd($device->id, $device->clientId, $cmd);
            }
        }

        return 'ok';
    }

    # 调表
    public function speed()
    {
        $mac = $this->request->input('mac');
        $device = Device::where('mac', $mac)->first();
        $speed = $this->request->input('speed');

        if ($device->clientId) {
             if ($device->mMac) {
            //     $cache = $this->container->get(\Psr\SimpleCache\CacheInterface::class);
            //     go(function () use ($mac, $device, $cache, $speed) {
            //         # 下发连接命令
            //         $this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "01");
            //         $cache->delete($mac . '-close');
            //         #sleep(1);
            //         # 查询连接状态
            //         #$this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "02");
            //         $loop = 0;
            //         $isSend = false;
            //         while ($loop < 30) {
            //             Coroutine::sleep(1);
            //             $loop ++;
            //             Logger::get()->notice('mac state', [$cache->get($mac)]);
            //             if ($cache->get($mac) == '01' && !$isSend) {
                            # 下发调表
                            $speed = strtoupper(dechex($speed));
                            $data = "02" . "03" . $this->deviceMqttService->toLen($speed, 8);
                            $cmd = $device->mac . "10" . "03" . $this->deviceMqttService->toLen(strlen($data) / 2, 2) . $data;
                            $this->sendCmd($device->id, $device->clientId, $cmd);
                            # 保存参数
                            $data = "02" . "FE" . $this->deviceMqttService->toLen($speed, 8);
                            $cmd = $device->mac . "10" . "03" . $this->deviceMqttService->toLen(strlen($data) / 2, 2) . $data;
                            $this->sendCmd($device->id, $device->clientId, $cmd);
                            $isSend = true;
                //             $cache->delete($mac);
                //             # 查询
                //             #sleep(1);
                //             #$cmd = $device->mac . "0C";
                //             #$this->sendCmd($device->id, $device->clientId, $cmd);
                //             #break;
                //         }
                //         if (!$isSend) {
                //             # 下发连接命令
                //             $this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "01");
                //         }
                //         if ($loop >= 30 && !$cache->get($mac . '-close')) {
                //             # 下发断开连接
                //             $this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "00");
                //         }
                //         if ($isSend && $cache->get($mac . '-close')) {
                //             break;
                //         }
                //     }
                // });
            } else {
                # 下发调表
                $speed = strtoupper(dechex($speed));
                $cmd = $device->mac . "02" . "03" . $this->deviceMqttService->toLen($speed, 8);
                $this->sendCmd($device->id, $device->clientId, $cmd);
                # 保存
                $cmd = $device->mac . "02" . "FE" . $this->deviceMqttService->toLen($speed, 8);
                $this->sendCmd($device->id, $device->clientId, $cmd);
            }
        }

        return 'ok';
    }



   public function curr()
    {
        $mac = $this->request->input('mac');
        $device = Device::where('mac', $mac)->first();
        $warning_curr = $this->request->input('warning_curr');
        Normal::logger('warning_curr---'.$warning_curr,'order-queue');
        if ($device->clientId) {
             if ($device->mMac) {
            //     $cache = $this->container->get(\Psr\SimpleCache\CacheInterface::class);
            //     go(function () use ($mac, $device, $cache, $speed) {
            //         # 下发连接命令
            //         $this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "01");
            //         $cache->delete($mac . '-close');
            //         #sleep(1);
            //         # 查询连接状态
            //         #$this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "02");
            //         $loop = 0;
            //         $isSend = false;
            //         while ($loop < 30) {
            //             Coroutine::sleep(1);
            //             $loop ++;
            //             Logger::get()->notice('mac state', [$cache->get($mac)]);
            //             if ($cache->get($mac) == '01' && !$isSend) {
                            # 下发调表
                            $warning_curr = strtoupper(dechex($warning_curr));
                            $data = "02" . "06" . $this->deviceMqttService->toLen($warning_curr, 8);
                            $cmd = $device->mac . "10" . "03" . $this->deviceMqttService->toLen(strlen($data) / 2, 2) . $data;
                            $this->sendCmd($device->id, $device->clientId, $cmd);
                            # 保存参数
                            $data = "02" . "FE" . $this->deviceMqttService->toLen($warning_curr, 8);
                            $cmd = $device->mac . "10" . "03" . $this->deviceMqttService->toLen(strlen($data) / 2, 2) . $data;
                            $this->sendCmd($device->id, $device->clientId, $cmd);
                            $isSend = true;
                //             $cache->delete($mac);
                //             # 查询
                //             #sleep(1);
                //             #$cmd = $device->mac . "0C";
                //             #$this->sendCmd($device->id, $device->clientId, $cmd);
                //             #break;
                //         }
                //         if (!$isSend) {
                //             # 下发连接命令
                //             $this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "01");
                //         }
                //         if ($loop >= 30 && !$cache->get($mac . '-close')) {
                //             # 下发断开连接
                //             $this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "00");
                //         }
                //         if ($isSend && $cache->get($mac . '-close')) {
                //             break;
                //         }
                //     }
                // });
            } else {
                # 下发调表
                $warning_curr = strtoupper(dechex($warning_curr));
                $cmd = $device->mac . "02" . "06" . $this->deviceMqttService->toLen($warning_curr, 8);
                $this->sendCmd($device->id, $device->clientId, $cmd);
                # 保存
                $cmd = $device->mac . "02" . "FE" . $this->deviceMqttService->toLen($warning_curr, 8);
                $this->sendCmd($device->id, $device->clientId, $cmd);
            }
        }

        return 'ok';
    }



    public function setTotal()
    {
        $mac = $this->request->input('mac');
        $device = Device::where('mac', $mac)->first();
        $value = $this->request->input('value');
        $value = $value*1000;

        if ($device->clientId) {
            if ($device->mMac) {
                // $cache = $this->container->get(\Psr\SimpleCache\CacheInterface::class);
                // go(function () use ($mac, $device, $cache, $type) {
                //     # 下发连接命令
                //     $this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "01");
                //     $cache->delete($mac . '-close');
                //     #sleep(1);
                //     # 查询连接状态
                //     #$this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "02");
                //     $loop = 0;
                //     $isSend = false;
                //     while ($loop < 30) {
                //         // sleep(1);
                //         Coroutine::sleep(1);
                //         $loop ++;
                //         Logger::get()->notice('mac state', [$cache->get($mac)]);
                //         if ($cache->get($mac) == '01' && !$isSend) {
                            # 下发模式
                            $value = strtoupper(dechex($value));
                            $data = "02" . "08" . $this->deviceMqttService->toLen($value, 8);
                            $cmd = $device->mac . "10" . "03" . $this->deviceMqttService->toLen(strlen($data) / 2, 2) . $data;
                            $this->sendCmd($device->id, $device->clientId, $cmd);
                            # 保存参数
                            $data = "02" . "FE" . $this->deviceMqttService->toLen($value, 8);
                            $cmd = $device->mac . "10" . "03" . $this->deviceMqttService->toLen(strlen($data) / 2, 2) . $data;
                            $this->sendCmd($device->id, $device->clientId, $cmd);
                            $isSend = true;
                //             $cache->delete($mac);
                //             # 查询
                //             #sleep(1);
                //             #$cmd = $device->mac . "0C";
                //             #$this->sendCmd($device->id, $device->clientId, $cmd);
                //             #break;
                //         }
                //         if (!$isSend) {
                //             # 下发连接命令
                //             $this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "01");
                //         }
                //         if ($loop >= 30 && !$cache->get($mac . '-close')) {
                //             # 下发断开连接
                //             $this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "00");
                //         }
                //         if ($isSend && $cache->get($mac . '-close')) {
                //             break;
                //         }
                //     }
                // });
            } else {
                # 下发模式
                $value = strtoupper(dechex($value));
                $cmd = $device->mac . "02" . "08" . $this->deviceMqttService->toLen($value, 8);
                $this->sendCmd($device->id, $device->clientId, $cmd);
                # 保存
                $cmd = $device->mac . "02" . "FE" . $this->deviceMqttService->toLen($value, 8);
                $this->sendCmd($device->id, $device->clientId, $cmd);
            }
        }

        return 'ok';
    }

//修改计量模式
    public function setMode()
    {
        $mac = $this->request->input('mac');
        $device = Device::where('mac', $mac)->first();
        $type = $this->request->input('type');

        if ($device->clientId) {
            if ($device->mMac) {
                // $cache = $this->container->get(\Psr\SimpleCache\CacheInterface::class);
                // go(function () use ($mac, $device, $cache, $type) {
                //     # 下发连接命令
                //     $this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "01");
                //     $cache->delete($mac . '-close');
                //     #sleep(1);
                //     # 查询连接状态
                //     #$this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "02");
                //     $loop = 0;
                //     $isSend = false;
                //     while ($loop < 30) {
                //         // sleep(1);
                //         Coroutine::sleep(1);
                //         $loop ++;
                //         Logger::get()->notice('mac state', [$cache->get($mac)]);
                //         if ($cache->get($mac) == '01' && !$isSend) {
                            # 下发模式
                            $type = strtoupper(dechex($type));
                            $data = "02" . "02" . $this->deviceMqttService->toLen($type, 2);
                            $cmd = $device->mac . "10" . "03" . $this->deviceMqttService->toLen(strlen($data) / 2, 2) . $data;
                            $this->sendCmd($device->id, $device->clientId, $cmd);
                            # 保存参数
                            $data = "02" . "FE" . $this->deviceMqttService->toLen($type, 2);
                            $cmd = $device->mac . "10" . "03" . $this->deviceMqttService->toLen(strlen($data) / 2, 2) . $data;
                            $this->sendCmd($device->id, $device->clientId, $cmd);
                            $isSend = true;
                //             $cache->delete($mac);
                //             # 查询
                //             #sleep(1);
                //             #$cmd = $device->mac . "0C";
                //             #$this->sendCmd($device->id, $device->clientId, $cmd);
                //             #break;
                //         }
                //         if (!$isSend) {
                //             # 下发连接命令
                //             $this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "01");
                //         }
                //         if ($loop >= 30 && !$cache->get($mac . '-close')) {
                //             # 下发断开连接
                //             $this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "00");
                //         }
                //         if ($isSend && $cache->get($mac . '-close')) {
                //             break;
                //         }
                //     }
                // });
            } else {
                # 下发模式
                $type = strtoupper(dechex($type));
                $cmd = $device->mac . "02" . "02" . $this->deviceMqttService->toLen($type, 2);
                $this->sendCmd($device->id, $device->clientId, $cmd);
                # 保存
                $cmd = $device->mac . "02" . "FE" . $this->deviceMqttService->toLen($type, 2);
                $this->sendCmd($device->id, $device->clientId, $cmd);
            }
        }

        return 'ok';
    }


    public function querySpeed()
    {
        $mac = $this->request->input('mac');
        $device = Device::where('mac', $mac)->first();

        if ($device->clientId) {
            if ($device->mMac) {
                // $cache = $this->container->get(\Psr\SimpleCache\CacheInterface::class);
                // go(function () use ($mac, $device, $cache) {
                //     # 下发连接命令
                //     $this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "01");
                //     $cache->delete($mac . '-close');
                //     $loop = 0;
                //     $isSend = false;
                //     while ($loop < 30) {
                //         // sleep(1);
                //         Coroutine::sleep(1);
                //         $loop ++;
                //         Logger::get()->notice('mac state', [$cache->get($mac)]);
                //         if ($cache->get($mac) == '01' && !$isSend) {
                            # 下发查表
                            $data = "02" . "83" . $this->deviceMqttService->toLen(0, 8);
                            $cmd = $device->mac . "10" . "03" . $this->deviceMqttService->toLen(strlen($data) / 2, 2) . $data;
                            $this->sendCmd($device->id, $device->clientId, $cmd);
                            $isSend = true;
                //             $cache->delete($mac);
                //             #break;
                //         }
                //         if (!$isSend) {
                //             # 下发连接命令
                //             $this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "01");
                //         }
                //         if ($loop >= 30 && !$cache->get($mac . '-close')) {
                //             # 下发断开连接
                //             $this->sendCmd($device->id, $device->clientId, $device->mac . "10" . "00");
                //         }
                //         if ($isSend && $cache->get($mac . '-close')) {
                //             break;
                //         }
                //     }
                // });
            } else {
                # 下发调表
                $cmd = $device->mac . "02" . "83" . $this->deviceMqttService->toLen(0, 8);
                $this->sendCmd($device->id, $device->clientId, $cmd);
            }
        }

        return 'ok';
    }

   public function sendCmd1($deviceId, $topic, $cmd, $qos = 1)
    {
        /*$service = $this->container->get(MqttClientService::class);
        $result = $service->client->publish($clientId, $this->deviceMqttService->returnData($cmd), $qos);
        if(!$result) {
            Logger::get()->error('mqtt publish fail', ['clientId' => $clientId,
                'msg' => $this->deviceMqttService->returnData($cmd), 'result' => $result]);
            $service->client->connect();
        }
        Logger::get()->notice('published', ['clientId' => $clientId,
            'msg' => $this->deviceMqttService->returnData($cmd), 'result' => $result]);*/
        DevicePublish::create([
            'device_id' => $deviceId,
            'topic' => $topic,
            'data' => $this->deviceMqttService->returnData1($cmd),
            'qos' => $qos
        ]);
    }

    public function sendCmd($deviceId, $topic, $cmd, $qos = 1)
    {
        /*$service = $this->container->get(MqttClientService::class);
        $result = $service->client->publish($clientId, $this->deviceMqttService->returnData($cmd), $qos);
        if(!$result) {
            Logger::get()->error('mqtt publish fail', ['clientId' => $clientId,
                'msg' => $this->deviceMqttService->returnData($cmd), 'result' => $result]);
            $service->client->connect();
        }
        Logger::get()->notice('published', ['clientId' => $clientId,
            'msg' => $this->deviceMqttService->returnData($cmd), 'result' => $result]);*/
        DevicePublish::create([
            'device_id' => $deviceId,
            'topic' => $topic,
            'data' => $this->deviceMqttService->returnData($cmd),
            'qos' => $qos
        ]);
    }

    public function testRedis(){
        $this->redis->select(2);
        $this->redis->setex('test_tcp_key', 3600 * 24 * 730, 1);
    }
}
